package com.rt.sms.controller.paopao;

import com.rt.sms.domain.SmsSendInfo;
import com.rt.sms.service.SmsSendInfoService;
import com.rt.sms.utils.RTLogger;
import com.rt.sms.utils.SendMessageUtil;
import com.rt.sms.utils.SmsTokenUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("paopao")
public class SmgwController {

    @Autowired
    SmsSendInfoService smsSendInfoService;

    @RequestMapping("sendSms")
    @ResponseBody
    public Map<String,Object> getSmsCode(HttpServletRequest request){
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {

        }
        Map map = new HashMap();
        map.put("status","1");
        map.put("msg","发送失败");
        String phone = request.getParameter("phone");
        String content = request.getParameter("content");

        RTLogger.getLogger("SMSCenterLog").debug("[接收融托子平台的请求]phone:"+phone+";content="+content);

        if(StringUtils.isBlank(phone)){
            map.put("status","1");
            map.put("msg","手机号不能为空");
            return map;
        }

        if(StringUtils.isBlank(content)){
            map.put("status","1");
            map.put("msg","内容不能为空");
            return map;
        }

        /** 1.来源校验 **/
        if(!SmsTokenUtil.checkToken(request)){
            map.put("status","1");
            map.put("msg","非法请求");
            return map;
        }

        // 短信发送
        String ppResult = SendMessageUtil.postUrl(content,phone);
        // {"status":"0","balance":2499950,"list":[{"mid":"BD4D63C3837EF002","mobile":"15106929160","result":0}]}
        JSONObject object = JSONObject.fromObject(ppResult);

        String mid = "";
        String mobile = phone;
        String result = "";
        String status = object.getString("status");
        if(status.equals("0")){
            JSONArray array = object.getJSONArray("list");
            if(array!=null && array.size()>0){
                JSONObject child = array.getJSONObject(0);
                mid = child.getString("mid");
                mobile = child.getString("mobile");
                result = child.getString("result");

                map.put("status","0");
                map.put("msg","发送成功");
            }
        }

        SmsSendInfo smsSendInfo = new SmsSendInfo();
        smsSendInfo.setMobile(mobile);
        smsSendInfo.setContent(content);
        smsSendInfo.setSendTime(new Date());
        smsSendInfo.setMid(mid);
        smsSendInfo.setResult(result);
        smsSendInfo.setStatus(status);

        smsSendInfoService.saveSmsSendInfo(smsSendInfo);

        return map;
    }
}